﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Diagnostics;
using System.ComponentModel;
using Lilium.Configuration;

namespace Lilium.Web.Mvc.Configuration
{
	public class EmbeededResourceElement: ScriptResourceElement
	{
		public override ScriptResource GetScriptResource()
		{
			return new EmbeededScriptResource(
				Name,
				GetResourceAssembly(),
				GetResourceName(),
				GetContentType(),
				GetOutputCachePolicy());
		}

		#region public string ResourceAssembly

		private const string ResourceAssemblyPropertyName = "resourceAssembly";

		[ConfigurationPropertyAttribute(ResourceAssemblyPropertyName)]
		[TypeConverter(typeof(AssemblyNameConverter))]
		public Assembly ResourceAssembly { [DebuggerNonUserCodeAttribute] get { return (Assembly)base[ResourceAssemblyPropertyName]; } [DebuggerNonUserCodeAttribute] set { base[ResourceAssemblyPropertyName] = value; } }

		#endregion

		protected internal Assembly GetResourceAssembly()
		{
			var library = ScriptResourceLibrary as EmbeededResourceLibraryElement;
			return ResourceAssembly != null || library == null ? ResourceAssembly : library.ResourceAssembly;
		}

		protected internal string GetResourceName()
		{
			var library = ScriptResourceLibrary as EmbeededResourceLibraryElement;
			return library == null ? Name : library.ResourceNamespace + "." + Name;
		}
	}
}